import pygame
from pygame.locals import *
from random import randint

class AnimationQueue:
	def __init__(self):
		self.data = []
		
	def add(self, event):
		self.data.append(event)
	
	def waiting(self):
		if len(self.data) > 0:
			return True
		else:
			return False

	def prune(self):
		for e in self.data:
			if e.alive == False:
				self.data.remove(e)
			
class AnimationEvent:
	def __init__(self):
		pass
		
	def draw(self):
		pass

	
class ScreenMessage(AnimationEvent):
	def __init__(self, message, time, loc):
		AnimationEvent.__init__(self)
		print 'created ScreenMessage() with message:' + message + ' and a time of '+ str(time) + 'ms.'
		self.font = pygame.font.Font(pygame.font.get_default_font(), 12)
		self.image = self.font.render(message, False, (255,255,255))
		self.rect = self.image.get_rect()
		self.rect.center = loc
		self.image_new = self.image
		self.alpha_incr = time/255
		self.alpha = 255
		self.size_incr = time/10
		self.size = 1.0
		self.next_fade = pygame.time.get_ticks()+self.alpha_incr
		self.next_grow = pygame.time.get_ticks()+self.size_incr
		self.screen = pygame.display.get_surface()
		self.endtime = pygame.time.get_ticks()+time
		print pygame.time.get_ticks()
		print self.endtime
		self.alive = True
		self.loc = loc
		
	def draw(self):
		self.image_new.set_alpha(self.alpha)
		self.screen.blit(self.image_new, self.rect)
		
	def update(self):
		if pygame.time.get_ticks() >= self.next_fade:
			self.alpha -= 1
			self.next_fade = pygame.time.get_ticks() + self.alpha_incr
		if pygame.time.get_ticks() >= self.next_grow:
			self.size += 0.01
			self.image_new = pygame.transform.scale(self.image, 
						(self.rect.w*self.size, self.rect.h*self.size))
			self.next_grow = pygame.time.get_ticks() + self.size_incr
			self.rect = self.image_new.get_rect()
		self.rect.center = self.loc
		if pygame.time.get_ticks() >= self.endtime:
			print 'event expired'
			self.alive = False

pygame.init()
screen = pygame.display.set_mode((800,600), FULLSCREEN)

run = True
aq = AnimationQueue()
aq.add(ScreenMessage("Turnover on downs!", 1000, (randint(0,300),randint(0,300)) ))

while run:
	screen.fill((0,0,0))
	for event in pygame.event.get():
		if event.type == pygame.QUIT: run = False
		elif event.type == KEYDOWN:
				if event.key == K_ESCAPE:
					run = False
				if event.key == K_q:
					aq.add(ScreenMessage("pressed the q key", 1000, (randint(0,800),randint(0,600)) ))
	rects = []
	if aq.waiting():
		for e in aq.data:
			e.draw()
			e.update()
			rects.append(e.rect)
		aq.prune()
		pygame.display.update(rects)
	else:
		pygame.display.flip()